<?php

class Proposals_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = "mis_proposals";
    }

    function newProposal($added_by, $path) {
        $data = $_POST;
        $data['file_path'] = $path;
        $data['added_by'] = $added_by;
        $this->db->insert($this->tableName, $data);
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }

    function getOne($tid) {
        $data['tid'] = $tid;
        $this->db->select("tid,title,date_advert,source,contact,added_by,deadline_date, sent_to, date_sent,value,file_path,date_added");
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $row['date_sent'] = $this->functions_model->prettyDate($row['date_sent']);
            $row['date_advert'] = $this->functions_model->prettyDate($row['date_advert']);
            $row['deadline_date'] = $this->functions_model->prettyDate($row['deadline_date']);
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            $row['file_path'] = "<a href=" . base_url() . "uploads/" . $row['file_path'] . ">Download</a>";
            $row['more'] = "<a href='javascript:loadUrl(\"#proposal_result\",\"" . base_url() . "index.php?/more_info/proposalsMoreInfo/" . $row['tid'] . "\")'>More details</a>";
            $result[] = $row;
        }
        return $result;
    }

//    function getAll($username) {
//        //$data['added_by'] = $username;
//        $this->db->select("tid, title, sent_to, date_sent,date_advert,file_path"); //,source,deadline_date,,contact
//        // $this->db->where($data);
//        $this->db->from($this->tableName);
//        $this->db->order_by("tid", "desc");
//        $result = array();
//        $query = $this->db->get();
//        foreach ($query->result_array() as $row) {
//            $row['date_sent'] = $this->functions_model->prettyDate($row['date_sent']);
//            $row['date_advert'] = $this->functions_model->prettyDate($row['date_advert']);
//            //   $row['deadline_date'] = $this->functions_model->prettyDate($row['deadline_date']);
//            //  $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
//            $row['file_path'] = "<a href=" . base_url() . "uploads/" . $row['file_path'] . ">Download</a>";
//            $row['more'] = "<a href='javascript:loadUrl(\"#proposal_result\",\"" . base_url() . "index.php?/more_info/proposalsMoreInfo/" . $row['tid'] . "\")'>More details</>";
//            unset($row['tid']);
//            $result[] = $row;
//        }
//        return $result;
//    }

    function adminGetAll($field=NULL, $search=NULL) {
        if ($field and $search) {
            if ($field == 'all') {
                $this->db->like("title", $search);
                $this->db->or_like("contact", $search);
                $this->db->or_like("source", $search);
                $this->db->or_like("sent_to", $search);
                $this->db->or_like("added_by", $search);
            } else {
                $data[$field] = $search;
                $this->db->like($data);
            }
        }
        $this->db->select("tid, date_added, title, sent_to, date_sent,date_advert,file_path");
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $result = array();
        $query = $this->db->get();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);
            $row['date_sent'] = $this->functions_model->prettyDate($row['date_sent']);
            $row['date_advert'] = $this->functions_model->prettyDate($row['date_advert']);
        //    $row['deadline_date'] = $this->functions_model->prettyDate($row['deadline_date']);
            $row['file_path'] = "<a href=" . base_url() . "uploads/" . $row['file_path'] . ">Download</a>";
            $row['more'] = "<a href='javascript:loadUrl(\"#proposal_result\",\"" . base_url() . "index.php?/more_info/proposalsMoreInfo/" . $row['tid'] . "\")'>More details</>";

            if ($this->session->userdata("admin") != FALSE) {
                $row['more'].="  <a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_mis/deleteProposal/$tid\",\"Are you sure to delete this record?\")'>Delete</a> ";
            }
            unset($row['tid']);
            $result[] = $row;
        }
        return $result;
    }

}

?>
